import { NextApiRequest, NextApiResponse } from "next";
import prisma from "../../../lib/prisma";

export default async function handle(
    req: NextApiRequest,
    res: NextApiResponse
) {
    const { name = "", email = "", page = 1, pageSize = 5 } = req.body;

    const total = await prisma.user.count({
        where: {
            AND: [
                {
                    name: {
                        contains: name,
                    },
                },
                {
                    email: {
                        contains: email,
                    },
                },
            ],
        },
    });

    const result = await prisma.user.findMany({
        where: {
            AND: [
                {
                    name: {
                        contains: name,
                    },
                },
                {
                    email: {
                        contains: email,
                    },
                },
            ],
        },
        skip: (page - 1) * pageSize,
        take: pageSize,
    });

    res.json({
        code: 200,
        total,
        data: result,
    });
}
